import express from "express";

import * as articles from '../../controllers/articles.js';
import * as categories from '../../controllers/categories.js'
import * as comment from '../../controllers/comment.js'
import { count } from 'console';
const router = express.Router();


router.post("/delete",async(req,res)=>{
    let id = req.body?.id ?? "";
    if (id==""){
        res.send({
            code:400,
            msg:"参数有误"
        })
        return;
    }
    let cate = await articles.selectArticlesById(id)
    if(cate.length!=0){
        res.send({
            code:200,
            msg:"关联分类文章不可以删哦"
        })
        return;
    }
    await categories.deleteCategoryById(id)
    res.send({
        code:200,
        msg:"删除成功"
    })
})

router.get("/list",async(req,res)=>{
    let data = await categories.selectCategoriesList()
    res.send({
        code:200,
        msg:"查询成功",
        data:data
    })
})

router.post("/add",async(req,res)=>{
    let name = req.body?.name ?? "";
    let slug = req.body?.name ?? "";
    if(name==""||slug==""||name!=slug){
        res.send({
            code:400,
            msg:"增加失败"
        })
        return;
    }
   await categories.addCategory(name,slug)
    res.send({
        code:200,
        msg:"增加成功",
    })
})

router.get("/search",async(req,res)=>{
    let id = req.body?.id ?? "";
    if(id==""){
        res.send({
            code:400,
            msg:"没有该文章哦"
        })
        return
    }
    let data = await categories.selectSearchCategory(id)
    res.send({
        code:200,
        msg:"查询成功",
        data:data
    })
})

export default router;